Ik voeg een invoerformulier in twee verschillende tabellen in. de eerste tabel is voor de informatie en de andere tabel is voor afbeeldingen van de informatie. Wanneer ik de informatie verzend, wil ik meerdere afbeeldingen per rij van de eerste tabel in een aparte tabel uploaden en ook last_insert_id aan elke afbeelding in de tweede tabel invoegen. tafel 1 locatie-ID | adres | contactgegevens tafel 2 afbeeldingID | locatie-ID | bestandspad Met de volgende code voegt het slechts één afbeelding in, maar het uploadt alle afbeeldingen naar de map. php include_once 'dbconnect.php'; mysql_query ("STEL NAMEN UTF8 IN"); session_start (); $ tbl_name = "locatie"; $ tbl_image = "afbeelding"; if (isset ($ _ POST ['btn-upload'])) { $ name2 = $ _ POST ['naam2']; $ phone = $ _ POST ['telefoon']; $ email = $ _ POST ['email']; $ type = $ _ POST ['type']; $ other = $ _ POST ['other']; $ description = $ _ POST ['description']; $ address = $ _ POST ['adres']; $ name = $ _ POST ['naam']; $ lat = $ _ POST ['lat']; $ lng = $ _ POST ['lng']; $ country = $ _ POST ['land']; $ Administrative_area_level_1 = $ _ POST ['Administrative_area_level_1']; $ place_id = $ _ POST ['place_id']; $ url = $ _ POST ['url']; $ website = $ _ POST ['website']; $ sql = "VOEG IN $ tbl_name (naam2, telefoon, e-mail, type, anders, beschrijving, adres, naam, lat, lng, land, administratief gebied_level_1, plaats_id, url, website) WAARDEN ('$ naam2', '$ telefoon' , '$ email', '$ type', '$ other', '$ description', '$ address', '$ name', '$ lat', '$ lng', '$ country', '$ Administrative_area_level_1' , '$ place_id', '$ url', '$ website') "; $ resultaat = mysql_query ($ sql); voor ($ i = 0; $ iphp } anders { ?> php } } if ($ resultaat) { echo "Succesvol"; echo "
"; echo " Terug naar hoofdpagina "; } anders { echo "ERROR"; } if ($ resultaat1) { echo "Upload succesvol"; echo "
"; echo " Terug naar hoofdpagina "; } anders { echo "ERROR"; } } ?> php // hechte band mysql_close (); ?>
2020-12-07 22:37:52
$ b = $ pdo-> voorbereiden ("INVOEGEN IN` tafel1` SET `adres` =: adres,` contact` =: contact "); $ b-> bindParam (": adres", $ adres); $ b-> bindParam (": contact", $ contact); $ b-> execute (); $ LastId = $ pdo-> lastInsertId (); if ($ LastId> 0) { $ b = $ pdo-> prepar ("INSERT INTO` table2` SET `pictureID` =: pictureID,` filepath` =: filepath "); $ b-> bindParam (": pictureID", $ LastId); $ b-> bindParam (": filepath", $ filepath); $ b-> execute (); } | Eindelijk bedacht. Het was de last_insert_ID die een probleem had. Last_instert_ID moest buiten de For-lus worden opgegeven. $ locationID = mysql_insert_id ($ conn); Hierboven wordt direct onder de eerste invoegquery toegevoegd. $ sql = "VOEG IN $ tbl_location (naam2, telefoon, e-mail, type, anders, beschrijving, adres, naam, lat, lng, land, administratief_gebied_level_1, plaats_id, url, website) WAARDEN ('$ name2', '$ phone', '$ email', '$ type', '$ other', '$ description', '$ address', '$ name', '$ lat', '$ lng ',' $ land ',' $ administratief_gebied_level_1 ',' $ place_id ',' $ url ',' $ website ') "; $ resultaat = mysql_query ($ sql); $ locationID = mysql_insert_id ($ conn); voor ($ i = 0; $ iphp } anders { ?> php } } | // hoe meerdere afbeeldingen in de database te uploaden, maar afbeelding opslaan in map met uploadtabel met laatste invoeg-id met lus // table1 - table1 ID | veld1 | veld2 table2 - uploaden afbeeldingID | ID | afbeelding uploaden php include ("config.php"); if (isset ($ _ POST ['submit'])) { $ field1 = $ _ POST ['field1']; $ field2 = $ _ POST ['field2']; $ sql = mysql_query ("INSERT IN TO table1 (field1, field2) VALUES ('field1', 'field2')"); // recordtabel 1 invoegen if ($ sql) { $ last = mysql_insert_id (); } if (isset ($ _ FILES ['uploadimage'] ['name'])) // uploadimage bestandsnaam en tabelkolomnaam hetzelfde hier gebruikt { $ file_name_all = ""; voor ($ i = 0; $ i | Uw antwoord StackExchange.ifUsing ("editor", function () { StackExchange.using ("externalEditor", function () { StackExchange.using ("snippets", function () { StackExchange.snippets.init (); }); }); }, "code-snippets"); StackExchange.ready (function () { var channelOptions = { tags: "" .split (""), id: "1" }; initTagRenderer ("". split (""), "" .split (""), channelOptions); StackExchange.using ("externalEditor", function () { // Moet de editor na fragmenten activeren, als fragmenten zijn ingeschakeld if (StackExchange.settings.snippets.snippetsEnabled) { StackExchange.using ("snippets", function () { createEditor (); }); } anders { createEditor (); } }); functie createEditor () { StackExchange.prepareEditor ({ useStacksEditor: false, heartbeatType: 'antwoord', autoActivateHeartbeat: false, convertImagesToLinks: waar, noModals: waar, showLowRepImageUploadWarning: true, ReputationToPostImages: 10, bindNavPrevention: true, postfix: "", imageUploader: { brandingHtml: "Aangedreven door \ u003ca href = \" https: //imgur.com/ \ "\ u003e \ u003csvg class = \" svg-icon \ "width = \" 50 \ "hoogte = \" 18 \ "viewBox = \ "0 0 50 18 \" fill = \ "none \" xmlns = \ "http: //www.w3.org/2000/svg \" \ u003e \ u003cpath d = \ "M46.1709 9.17788C46.1709 8.26454 46.2665 7.94324 47.10847.58816C47.4091 7.46349 47.7169 7.36433 48.0099 7.26993C48.9099 6.97997 49.672 6.73443 49.672 5.93063C49.672 5.22043 48.9832 4.61182 48.1414 4.61182C47.4335 4.61182 46.7256 4.91628 46.0943 5.50789C45.7307 ... ... 10.1419V6.59049C41.5985 5.28821 41.1394 4.66232 40.1061 4.66232C39.0732 4.66232 38.5948 5.28821 38.5948 6.59049V9.60062C38.5948 10.8521 38.2696 11.5455 37.0451 11.5455C35.8209 11.5458835.4956 ... 4.66232 32.492 5.28821 32.492 6.59049V10.1419Z \ "/ \ u003e \ u003cpath fill-rule = \" evenodd \ "clip-rule = \" evenodd \ "d = \" M25.6622 17.6335C27.8049 17.6335 29.3739 16.9402 30.2537 15.6379C30 .8468 1 4.7755 30.9615 13.5579 30.9615 11.9512V6.59049C30.9615 5.28821 30.4833 4.66231 29.4502 4.66231C28.9913 4.66231 28.4555 4.94978 28.1109 5.50789C27.499 4.86533 26.7335 4.56087 25.7005 4.56087C23.1369 4.56087C23.1369 2.56087 13,913 27,4607 13,4902 28,1109 12.6616C28.1109 12,7229 28,1161 12,7799 28,121 12.8346C28.1256 12,8854 28,1301 12,9342 28,1301 12.983C28.1301 14,4373 27,2502 15,2321 25,777 15.2321C24.8349 15,2321 24,1352 14,9821 23,5661 14.7787C23.176 14,6393 22,8472 14,5218 22,5437 14.5218C21.7977 14,5218 21,2429 15.0123 21.2429 15.6887C21.2429 16.7375 22.9072 17.6335 25.6622 17.6335ZM24.1317 9.27932C24.1317 7.94324 24.9928 7.09766 26.1024 7.09766C27.2119 7.09766 28.0918 7.94324 28.0918 9.27932C28.0918 26.0918 9.27932C28.0918 26.0918 9.27932C28.0918 11.611.93.26 '' / \ u003e \ u003cpath d = \ "M16.8045 11.9512C16.8045 13.2535 17.2637 13.8962 18.2965 13.8962C19.3298 13.8962 19.8079 13.2535 19.8079 11.9512V8.12928C19.8079 5.82936 18.4879 4.62866 16.4027 4.62866C15.1594 4.62866 14.279 4.98375 13.3609 5.88013C12.653 5.05154 11.6581 4.62866 10.3573 4.62866C9.34336 4.62866 8.57809 4.89931 7.9466 5.506.479C7.583 6.59049V11.9512C5.00066 13.2535 5.47873 13.8962 6.51203 13.8962C7.54479 13.8962 8.0232 13.2535 8.0232 11.9512V8.90741C8.0232 7.58817 8.44431 6.91179 9.53458 6.91179C10.5104 6.91179 10.893 7.58817 10.893.45.38 13.8962 13.9157 13.2535 13.9157 11.9512V8.90741C13.9157 7.58817 14.3365 6.91179 15.4269 6.91179C16.4027 6.91179 16.8045 7.58817 16.8045 8.94108V11.9512Z \ "/ \ u003e \ u003cpath d = \" M3.31675 6.59049C3.31675 6.59049C3.31675 6.59049C3.8.316 .791758 4.66232 0.313354 5.28821 0.313354 6.59049V11.9512C0.313354 13.2535 0.791758 13.8962 1.82471 13.8962C2.85798 13.8962 3.31675 13.2535 3.31675 11.9512V6.59049Z \ "/ \ u003e \ u003cpath d = \" M1.8 7209 0.400291C0.843612 0.400291 0 1.1159 0 1.98861C0 2.87869 0.822846 3.57676 1.87209 3.57676C2.90056 3.57676 3.7234 2.87869 3.7234 1.98861C3.7234 1.1159 2.90056 0.400291 1.87209 0.400291E "# 1 \ u00c3e" \ u003e \ u003c / a \ u003e ", contentPolicyHtml: "Gebruikersbijdragen in licentie gegeven onder \ u003ca href = \" https: //stackoverflow.com/help/licensing \ "\ u003ecc by-sa \ u003c / a \ u003e \ u003ca href = \" https://stackoverflow.com / legal / content-policy \ "\ u003e (contentbeleid) \ u003c / a \ u003e", allowUrls: waar }, onDemand: waar, discardSelector: ".discard-answer" , onmiddellijkShowMarkdownHelp: true, enableSnippets: true }); } }); Bedankt voor het bijdragen aan een antwoord op Stack Overflow! Zorg ervoor dat u de vraag beantwoordt. Geef details en deel uw onderzoek! Maar vermijd ... Om hulp, opheldering vragen of op andere antwoorden reageren. Uitspraken doen op basis van meningen; ondersteun ze met referenties of persoonlijke ervaring. Bekijk onze tips voor het schrijven van goede antwoorden voor meer informatie. Concept opgeslagen Concept verwijderd Meld u aan of log in StackExchange.ready (function () { StackExchange.helpers.onClickDraftSave ('# login-link'); }); Meld u aan met Google Meld u aan met Facebook Meld u aan met e-mail en wachtwoord Verzenden Post als gast Naam E-mail Vereist, maar nooit getoond StackExchange.ready ( functie () { StackExchange.openid.initPostLogin ('. New-post-login', 'https% 3a% 2f% 2fstackoverflow.com% 2fquestions% 2f43434118% 2fphp-mysql-multiple-upload-last-insert-id-for-loop% 23new- antwoord ',' vraagpagina '); } ); Post als gast Naam E-mail Vereist, maar nooit getoond Plaats uw antwoord Gooi weg Door op “Plaats uw antwoord” te klikken, gaat u akkoord met onze servicevoorwaarden, privacybeleid en cookiebeleid Niet het antwoord waar je naar zoekt? Blader door andere vragen met de tag php mysql of stel uw eigen vraag.